# 08_jes2010_wave5.R
# Purpose: Ideological Extremism and Political Participation in Japan
# Created: 2020-5-16 Taka-aki Asano
# Last Modified: 2021-10-9

# package
require("haven")
require("dplyr")
require("ltm")
require("plink")


# dataset
JES2010 <- read_sav(
  "JESIV/0999_wave5.sav", 
  user_na = FALSE
)


# respondent
JES2010_Respondent <- JES2010[,c("ID", paste0("Q", 17:25, "s1"))]
colnames(JES2010_Respondent)[-1] <- paste0("q", 17:25)
JES2010_Respondent <- apply(JES2010_Respondent, c(1, 2), 
                            function(x) {
                              if(x%in%c(8, 9)) {x <- NA} 
                              else {x <- x}}
                            ) %>% as.data.frame()


# IRT
## handle missing data
JES2010_Respondent <- JES2010_Respondent[rowSums(JES2010_Respondent[,-1], na.rm = TRUE) != 0,]

## estimate item parameters
irt2010 <- ltm::grm(JES2010_Respondent[,-1], IRT.param = FALSE)
irt2010 ## viewing

## estimate voters ideology
score2010_Respondent <- ltm::factor.scores(irt2010, JES2010_Respondent[,-1])


# rescale
## common item
common2012 <- c("q8", "q9", "q10", "q12", "q13", "q14", "q15", "q16")
common2010 <- c("q17", "q18", "q19", "q21", "q22", "q23", "q24", "q25")
index <- data.frame(
  group1 = match(common2012, colnames(JES2012_Respondent[,-1])), 
  group2 = match(common2010, colnames(JES2010_Respondent[,-1]))
)

## list of parameters
### 2012
res2012 <- coef(irt2012)
res2012 <- res2012[,c(4,1:3)]
colnames(res2012) <- c("a","b1","b2","b3")
### 2010
res2010 <- coef(irt2010)
res2010 <- res2010[,c(4,1:3)]
colnames(res2010) <- c("a","b1","b2","b3")
### merge
pm <- list(res2012, res2010)

## list of scale
rescat <- list(rep(4, nrow(res2012)), 
               rep(4, nrow(res2010)))

## as.irt.pars
pm2012 <- as.poly.mod(n = nrow(res2012), model = "grm", 
                      items = 1:nrow(res2012))
pm2010 <- as.poly.mod(n = nrow(res2010), model = "grm", 
                      items = 1:nrow(res2010))
res <- as.irt.pars(pm, common = index, cat = rescat, 
                   poly.mod = list(pm2012, pm2010), 
                   location = FALSE)

## list of voters' position
ideology <- list(score2012_Respondent$score.dat$z1, 
                 score2010_Respondent$score.dat$z1)

## rescale
plink_out <- plink(res, common = index, base.grp = 1, 
                   rescale = "MS", ability = ideology)
summary(plink_out)

## estimate voters ideology (rescale)
score2010_Respondent_plink <- link.ability(plink_out)$group2
JES2010_Respondent$Ideology <- score2010_Respondent_plink


# LDP's position
JES2010_LDP <- JES2010[,c("ID", paste0("Q", 17:25, "s3_1"))]
colnames(JES2010_LDP)[-1] <- paste0("q", 17:25)
JES2010_LDP <- apply(JES2010_LDP, c(1, 2), 
                     function(x) {
                       if(x%in%c(8, 9)) {x <- NA}
                       else {x <- x}}
                     ) %>% as.data.frame()
JES2010_LDP <- JES2010_LDP[rowSums(JES2010_LDP[,-1], na.rm = TRUE) != 0,]
score2010_LDP <- ltm::factor.scores(irt2010, JES2010_LDP[,-1])
ideology <- list(score2012_Respondent$score.dat$z1, 
                 score2010_LDP$score.dat$z1)
plink_out <- plink(res, common = index, base.grp = 1, 
                   rescale = "MS", ability = ideology)
JES2010_LDP$Ideology <- link.ability(plink_out)$group2


# DPJ's position
JES2010_DPJ <- JES2010[,c("ID", paste0("Q", 17:25, "s3_2"))]
colnames(JES2010_DPJ)[-1] <- paste0("q", 17:25)
JES2010_DPJ <- apply(JES2010_DPJ, c(1, 2), 
                     function(x) {
                       if(x%in%c(8, 9)) {x <- NA}
                       else {x <- x}}
                     ) %>% as.data.frame()
JES2010_DPJ <- JES2010_DPJ[rowSums(JES2010_DPJ[,-1], na.rm = TRUE) != 0,]
score2010_DPJ <- ltm::factor.scores(irt2010, JES2010_DPJ[,-1])
ideology <- list(score2012_Respondent$score.dat$z1, 
                 score2010_DPJ$score.dat$z1)
plink_out <- plink(res, common = index, base.grp = 1, 
                   rescale = "MS", ability = ideology)
JES2010_DPJ$Ideology <- link.ability(plink_out)$group2
